package ged.customizationCode.functions;

import java.awt.GraphicsEnvironment;

import ged.Link;
import ged.Node;

import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Display;

public class CustomizationCodeLinkEditPart {

	//constructor
	public CustomizationCodeLinkEditPart()
	{
		
	}	
	
	//change the type of the line
	public int changeLineType(Link link) {
		ged.LineType line_type = link.getConnection_lineType();
		int type = 0;
		switch (line_type) {
		case SOLID:
			type = SWT.LINE_SOLID;
			break;
		case DASH:
			type = SWT.LINE_DASH;
			break;
		case DASH_DOT:
			type = SWT.LINE_DASHDOT;
			break;
		case DASH_DOT_DOT:
			type = SWT.LINE_DASHDOTDOT;
			break;
		case DOT:
			type = SWT.LINE_DOT;
			break;
		default:
			type = SWT.LINE_CUSTOM;
			break;
		}
		return type;		
	}
	
	//set font of the label - bold - italic - underline - strikeThrough - fontName - fontHeight
	public void setLabelFont(WrappingLabel fFigureNodeLabelFigure, Link link) {
		ged.Font font = link.getFont();
		if (font != null) {
			//StrikeThrough 
			fFigureNodeLabelFigure.setTextStrikeThrough(font
					.isLabelStrikeThrough());
			// underline
			fFigureNodeLabelFigure
					.setTextUnderline(font.isLabelUnderline());
			// ged.fontName fontName = node.getLabel_fontName();
			String fontName = font.getFname();
			System.out.println(fontName + "Font Name");		    
		    
			//if the given font does not exist, set the default font "Tahoma"
			if(fontName == null)
				fontName = "";
		    if(!validFontName(fontName))
		    {
		    	System.out.println("The font does not exist");
	    		fontName = "Tahoma";
		    }

		    System.out.println("The font to apply is: " + fontName);
		    
			int height = font.getFontHeight();
			// change Font
			// BOLD and ITALIC
			if (font.isLabelBold() && font.isLabelItalic()) {
				fFigureNodeLabelFigure.setFont(
						new Font(Display.getCurrent(), fontName,
								(height != 0 ? height : 9), SWT.BOLD
										+ SWT.ITALIC));
			}
			// ITALIC
			else if (font.isLabelItalic()) {
				fFigureNodeLabelFigure.setFont(
						new Font(Display.getCurrent(), fontName,
								(height != 0 ? height : 9), SWT.ITALIC));
			}
			// BOLD
			else if (font.isLabelBold()) {
				fFigureNodeLabelFigure.setFont(
						new Font(Display.getCurrent(), fontName,
								(height != 0 ? height : 9), SWT.BOLD));
			} else {
				fFigureNodeLabelFigure.setFont(
						new Font(Display.getCurrent(), fontName,
								(height != 0 ? height : 9), SWT.NORMAL));
			}
		}
	}
	
	/**check if the given font is valid
	 * 
	 * @param thefontName
	 * @return
	 */
	public boolean validFontName(String thefontName)
	{
		//get all available fonts in the system
		GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
	    java.awt.Font[] fonts = e.getAllFonts(); // Get the fonts
	    
	  //compare the value of the attribute 
	    for(java.awt.Font f : fonts)
	    {
	    	String fontToString = f.getFontName();
	    	//if the given font exists in the available fonts
	    	if(fontToString.equals(null))
	    		return false;
	    	else if(thefontName.equals(fontToString))
		    	return true;
	    }
		return false;		
	}
}
